import greenfoot.*; 

/**
 * The airplane class contains the actor that drops the sandbags on the world
 * @author Jasper Jans
 */
public class Airplane extends Actor
{
    
    private int speed = 10;
    private int targetX = 0;
    private int targetY = 0;
    
    private boolean dropzoneFound = false;
    private boolean visible = false;
    private boolean dropped = false;
    
    private long startTime = System.currentTimeMillis();
    
    private Sandbag sandBag;

    public Airplane()
    {
        GreenfootSound sample = new GreenfootSound("sandbags-airplane.mp3");
        sample.setVolume(30);
        sample.play();
        
        // Set the airplane invisible as long as the sequence has not been started
        GreenfootImage img = getImage();
        img.setTransparency(0);
    
    }
    
    /*
     * Turns the airplane visible
     */
    private void showMe()
    {
        GreenfootImage img = new GreenfootImage("airplane.png");
        img.scale(img.getWidth() + 100, img.getHeight() + 100);
        setImage(img); 
        visible = true;
    }
    
    /*
     * Picks random coordinates and checks with the SandbagTarget actor to see if it allows a sandbag to be dropped here. 
     * If so, set the airplane to a location opposite to the found coordinates and turns the airplane in the direction of the dropzone
     */
    public void findDropZone()
    {
        
        showNextBagType();  // next sandbag to be thrown
        
        SandbagTarget target = new SandbagTarget();
        
        GreenfootImage img = target.getImage();
        img.setTransparency(0);
        target.setImage(img);
        
        getWorld().addObject(target, 0,0);
        
        do
        {
            targetX = Greenfoot.getRandomNumber(780);
            targetY = Greenfoot.getRandomNumber(480) + 100;
            
            
            target.setLocation(targetX, targetY); // set the target on the random coordinates
            if(target.confirmTarget()) // can I drop my sandbag here?
            {
                dropzoneFound = true;
            }
                                
        }while(!dropzoneFound);
        
        // set the airplane opposite to the dropzone to make it a flight accross the screen
        setLocation(
            targetX >= 400 ? 100 : 700, 
            targetY >= 300 ? 100 : 500);
        
            
        turnTowards(targetX, targetY);  // set the nose of the plane in the right direction
        
    }
    
    
    /*
     * Picks a random sandbagtype to be thrown next and shows it on top of the screen
     */
    private void showNextBagType()
    {
        int a = Greenfoot.getRandomNumber(10);
        int b = Greenfoot.getRandomNumber(10);
       
        if(a == b)
        {
            sandBag = new BigBag();
        }
        else if(a < b)
        {
            sandBag = new SmallBag();
        }
        else{
            sandBag = new Box();
        }
        
        sandBag.setAllowPickUp(false);  // player-actor is not allowed to pick up this sandbag 
        
        getWorld().addObject(sandBag, 400, 50);
        GreenfootImage img = sandBag.getImage();
        img.scale(img.getWidth() / 2, img.getHeight() / 2);
        sandBag.setImage(img);
 
        
    }
    
    /*
     * 1. Creates a next sandbag to be dropped. 
     * 2. Waits until the next dropzone is found. 
     * 3. When the dropzone is found fades the nextsandbagtype and show the plane.
     * 4. Moves plane to found coordinates
     * 5. Drops sandbag on found coordinates
     * 6. Removes this object when on edge of screen after drop
     */
    public void act() 
    {
        if(dropzoneFound)
        {
            long currentTime = System.currentTimeMillis();
            long elapsedTime = currentTime - startTime; 
            
            if(!visible)
            {
                if(elapsedTime / 1000 >= 2) // Wait 2 seconds before the plane comes in to view.
                {
                    showMe();
                    sandBag.fade();
                } 
                
                return;
            }
            else
            {
                
                move(speed);    // move towards dropzone
                
                if(dropped && (getX() <= 1 || getY() <= 1  || getX() >= 799 || getY() >=599)) // on edge? remove me
                {
                    getWorld().removeObject(this);
                    return;
                }
                
                // Reached the dropzone and drops the sandbag
                if(!dropped && dropzoneFound && 
                    (targetX - 50 <= getX() && targetX + 40 >=getX())&&
                    (targetY - 50 <= getY() && targetY + 50 >= getY()))
                {
                    sandBag.showMe();
                    sandBag.setLocation(targetX, targetY);
                    GreenfootImage img = sandBag.getImage();
                    img.scale(img.getWidth() * 2, img.getHeight() * 2);
                    sandBag.setImage(img);                    
                    
                    dropSandBag(sandBag);
                    sandBag.setAllowPickUp(true);
                    dropped = true;
                }
            }
        }else{
            if(sandBag == null) 
                showNextBagType();
        }
        
    }    
    
    /*
     * Drop the sandbag
     */
    private void dropSandBag(Sandbag sandBag)
    {
        dropped = true;
        getWorld().addObject(sandBag, getX(), getY());
        sandBag.drop();
    }
    
    
}
